package com.zlyx.easy.web.config;

import java.lang.reflect.Field;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.zlyx.easy.core.tool.FormatTool;
import com.zlyx.easy.database.marker.DaoMarker;

/**
 * @Auth 赵光
 * @Describle
 * @2019年1月14日 上午8:31:37
 */
@Configuration
public class MybatisPlusDaoConfiguration {

	@Bean
	@ConditionalOnMissingBean
	public DaoMarker daoMarker() {
		return new DaoMarker() {
			@Override
			public String parse(Field field) {
				TableField tableField = field.getAnnotation(TableField.class);
				TableId tableId = field.getAnnotation(TableId.class);
				if (tableField != null && !"".equals(tableField.value()) && tableField.value() != null) {
					return tableField.value();
				} else if (tableId != null && !"".equals(tableId.value()) && tableId.value() != null) {
					return tableId.value();
				} else if (isChange()) {
					return FormatTool.camel2Underline(field.getName());
				} else {
					return field.getName();
				}
			}
		};
	}
}
